﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SVN.Net.Misc
{
    public static class HtmlHelperPlus
    {

        public static string SubmitButton(this System.Web.Mvc.HtmlHelper helper, string name, string buttonText)
        {
            System.Web.Mvc.TagBuilder buttonTag = new System.Web.Mvc.TagBuilder("input");

            buttonTag.Attributes.Add("type", "submit");
            buttonTag.Attributes.Add("id", name);

            if (!String.IsNullOrEmpty(name))
                buttonTag.Attributes.Add("name", name);

            if (!String.IsNullOrEmpty(buttonText))
                buttonTag.Attributes.Add("value", buttonText);

            return buttonTag.ToString();
        }

        public static string ActionImageLink(this System.Web.Mvc.HtmlHelper helper, string imageSrc, string imageAlt, string actionName, string controllerName, object values, object htmlAttributes)
        {
            string ret = string.Empty;
            string url = string.Empty;

            System.Web.Routing.RouteValueDictionary valuesDir = new System.Web.Routing.RouteValueDictionary(values);
            IDictionary<string, object> htmlAttribs = null;
            if (htmlAttributes == null)
                htmlAttribs = new System.Web.Routing.RouteValueDictionary();
            else
                htmlAttribs = new System.Web.Routing.RouteValueDictionary(htmlAttributes);

            if (actionName != null)
            {
                if (valuesDir.ContainsKey("action"))
                    throw new ArgumentException("Duplcated defined.", "actionName");
                valuesDir.Add("action", actionName);
            }
            if (controllerName != null)
            {
                if (valuesDir.ContainsKey("controller"))
                {
                    throw new ArgumentException("Duplcated defined.", "controllerName");
                }
                valuesDir.Add("controller", controllerName);
            }

            System.Web.Routing.VirtualPathData vpd = System.Web.Routing.RouteTable.Routes.GetVirtualPath(helper.ViewContext, valuesDir);

            if (vpd != null)
            {
                // Get the action link text.
                url = vpd.VirtualPath;
                // Get the image text.
                System.Web.Mvc.TagBuilder builder = new System.Web.Mvc.TagBuilder("img");
                builder.Attributes.Add("src", imageSrc);
                builder.Attributes.Add("alt", imageAlt);
                string img = builder.ToString();
                // Get the action link text.
                builder = new System.Web.Mvc.TagBuilder("a");
                builder.Attributes.Add("href", url);
                foreach (var kvp in htmlAttribs)
                    builder.Attributes.Add(kvp.Key,kvp.Value.ToString());
                builder.InnerHtml = img;
                ret = builder.ToString();
            }

            return ret;

        }

        public static string ActionImageLink(this System.Web.Mvc.HtmlHelper helper, string imageSrc, string imageAlt, string actionName, string controllerName, object values)
        {
            return ActionImageLink(helper, imageSrc, imageAlt, actionName, controllerName, values, null);
        }
    }
}
